<?php

class Frontend_ErrorController extends Zend_Controller_Action
{
    public function errorAction()
    {
        $error = $this->_getParam('error_handler');

        switch ($error->type) {
            // 404 error - controller or action not found
            case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_CONTROLLER:
            case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_ACTION:
            case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_ROUTE:
                $this->getResponse()->setHttpResponseCode(404);
                $this->view->error = '404 Not Found';
            break;

        default:
            switch ($error->exception->getCode()) {
                // access error
                case 403:
                $this->getResponse()->setHttpResponseCode(403);
                $this->view->error = '403 Forbidden';
                    break;

                // application error
                default:
                $this->getResponse()->setHttpResponseCode(500);
                $this->view->message = '500 Internal Server Error';
            }
        }

        $this->view->exception = $error->exception;
        $this->view->request   = $error->request;

        // do not send error body on production in AJAX requests
        if (ZAPP_PRODUCTION === ZAPP_ENVIRONMENT && $this->getRequest()->isXmlHttpRequest()) {
            $this->getResponse()->sendHeaders();
            exit(1);
        }
    }
}
